﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FluidSystem
{
    public class BoundedGrid<T>
    {
        protected T[, ,] values;
        protected int xSize, ySize, zSize;

        public T x0 { get; private set; }
        public T xEnd { get; private set; }
        public T y0 { get; private set; }
        public T yEnd { get; private set; }
        public T z0 { get; private set; }
        public T zEnd { get; private set; }

        public BoundedGrid(int x, int y, int z)
        {
            xSize = x;
            ySize = y;
            zSize = z;
            values = new T[x, y, z];
        }

        /// <summary>
        /// valid elements are 0 thru Size - 1, bounds are -1 and size
        /// </summary>
        public virtual T this[int x,int y, int z]
        {
            get {
                if (x == -1)
                    return x0;
                else if (x == xSize)
                    return xEnd;
                else if (y == -1)
                    return y0;
                else if (y == ySize )
                    return yEnd;
                else if (z == -1)
                    return z0;
                else if (z == zSize )
                    return zEnd;
                else
                    return values[x, y, z];
            }
            set {
                if (x == -1)
                    throw new IndexOutOfRangeException("bound");
                else if (x == xSize )
                    throw new IndexOutOfRangeException("bound");
                else if (y == -1)
                    throw new IndexOutOfRangeException("bound");
                else if (y == ySize )
                    throw new IndexOutOfRangeException("bound");
                else if (z == -1)
                    throw new IndexOutOfRangeException("bound");
                else if (z == zSize )
                    throw new IndexOutOfRangeException("bound");
                else
                    values[x, y, z] = value; 
            }
        }


        internal virtual void SetBorders(T border)
        {
            x0 = border;
            xEnd = border;
            y0 = border;
            yEnd = border;
            z0 = border;
            zEnd = border;
        }
    }
}
